package app.fugema.shop.main.order.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.Observer;

import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewModel;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.OrderListAcBinding;

public class OrderListActivity extends BaseTitleActivity<OrderListAcBinding, BaseViewModel> {

    /**
     * @param context
     * @param source  0全部   1:待付款 2:待发货 3:待收货 4:待评价
     */
    public static void start(Context context, int source)
    {
        Intent intent = new Intent(context, OrderListActivity.class);
        intent.putExtra("source", source);
        context.startActivity(intent);
    }

    //订单状态  0全部 1:待付款  2:待发货  3:待收货   5:待评价/评价/        (未知) 5:成功 6:失败 false
    private enum Item {

        match("全部", OrderListFragment.getInstance(0)),
        home("待付款", OrderListFragment.getInstance(1)),
        live("待发货", OrderListFragment.getInstance(2)),
        note("待收货", OrderListFragment.getInstance(3)),

        myCenter("待评价", OrderListFragment.getInstance(5));

        public String name;
        public BaseFragment fragment;

        Item(String name, BaseFragment fragment) {
            fragment.setTitle(name);
            this.name = name;
            this.fragment = fragment;
        }

    }

    @Override
    protected void initNotificationReceipt() {
        super.initNotificationReceipt();
        //确认收货成功  跳转到 待评价  并弹出评价
        LiveEventBus.get(AppConstants.EventKey.ORDER_RECEIPT, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o) {
                binding.vpPager.setCurrentItem(4);
            }
        });
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_list_ac);
    }

    private int source;//跳转

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("订单列表",R.color.white);
        source = getIntent().getIntExtra("source", 0);
        binding.vpPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return Item.values()[position].fragment;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return Item.values()[position].name;
            }

            @Override
            public int getCount() {
                return Item.values().length;
            }
        });

        viewUtils.initSlidingTabLayout(binding.stlTab, binding.vpPager);

        if (source < Item.values().length)
        {
            binding.vpPager.setCurrentItem(source, false);
        }
    }

}
